package com.hxl.controller;


import cn.hutool.captcha.LineCaptcha;
import com.hxl.entity.CmfzAdmin;
import com.hxl.service.CmfzAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 * InnoDB free: 10240 kB 前端控制器
 * </p>
 *
 * @author hxl
 * @since 2020-08-18
 */
@Controller
@RequestMapping("/admin")
public class CmfzAdminController {
    @Autowired
    private CmfzAdminService cmfzAdminService;
    @RequestMapping("login")
    public String login(HttpSession session,String username,String password,String code){
        //通过账号和密码查询用户
        CmfzAdmin admin = cmfzAdminService.login(username, password);
        //将登陆信息放入session中
        session.setAttribute("admin",admin);
        //去出session中的验证码值
        String sessionCode = (String) session.getAttribute("code");
        //验证用户是否存在，验证码是否输入正确
        if(admin!=null && sessionCode.equals(code))
            return "redirect:/main.jsp";
        return "index.jsp";
    }
    @RequestMapping("getCaptcha")
    public void getCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
        //调用胡图工具类创建验证码
        LineCaptcha line = new LineCaptcha(80,30,4,8);
        //设置响应格式
        response.setContentType("image/jpg");
        //将验证码放入session作用域
        String code = line.getCode();
        session.setAttribute("code",code);
        //将图片放入流中
        ServletOutputStream outputStream = response.getOutputStream();
        line.write(outputStream);
    }
}
